import { connection } from '../app/database/mysql'
import { PostModel } from './post.model'
import { sqlFragment } from './post.provider'

export const getPostById = async (
    postId: number
) => {
    // 关联用户表 用户JSON 
    // 点赞总数
    // 评论总数
    const statement = `
        SELECT
            post.id,
            post.title,
            post.content,
            ${sqlFragment.user},
            ${sqlFragment.totalComments},
            ${sqlFragment.tags},
            ${sqlFragment.totalLikes}
            
        FROM post
        ${sqlFragment.leftJoinUser}
        WHERE post.id = ?
    `
    // 执行查询
    const [data] = await connection.promise().query(statement, postId)

    if (!data[0]) {
        throw new Error('NOT_FOUND')
    }

    return data[0]
}